package com.example.wuqujingdianphone.fragment.bookcity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.activity.AudioListActivity;
import com.example.wuqujingdianphone.activity.BookDetailActivity;
import com.example.wuqujingdianphone.activity.BookWebDetailActivity;
import com.example.wuqujingdianphone.activity.VideoListActivity;
import com.example.wuqujingdianphone.adapter.BCMultiMediaAdapter;
import com.example.wuqujingdianphone.adapter.BRBookAdapter;
import com.example.wuqujingdianphone.api.ApiConfig;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseFragment;
import com.example.wuqujingdianphone.bean.BookListBean;
import com.example.wuqujingdianphone.bean.MultiMediaBean;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.mode.CacheMode;
import com.vise.xsnow.http.mode.CacheResult;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yuanpk on 2018/4/9.
 * 书城---多媒体
 */

public class BCMultiMediaFragment extends BaseFragment {
    private static final String TAG = "BCMultiMediaFragment";
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private BCMultiMediaAdapter mAdapter;
    private String type;

    private boolean isFirstLoadDate;


    public static BCMultiMediaFragment getBCMultiMediaFragment(String type) {
        BCMultiMediaFragment fragment = new BCMultiMediaFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_city_multimedia, null);
        type = getArguments().getString("type");
        return view;
    }

    @Override
    protected void initData() {
        initView();
        isFirstLoadDate = true;

    }

    @Override
    protected void lazyLoad() {
        Log.i(TAG, "lazyLoad: type=" + type);
        if (isFirstLoadDate) {
            isFirstLoadDate = false;
            getData(type);
        }
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mAdapter = new BCMultiMediaAdapter(getActivity(), null);
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i(TAG, "onItemClick: position=" + position);
//                Intent intent = new Intent(getActivity(), BookWebDetailActivity.class);
//                intent.putExtra("type", type);
//                intent.putExtra("bookId", mAdapter.getItem(position).id);
//                startActivity(intent);
                if (type.equals("听书馆")) {
                    Intent intent = new Intent(getActivity(), AudioListActivity.class);
                    intent.putExtra("id", mAdapter.getItem(position).id);
                    intent.putExtra("title", mAdapter.getItem(position).news_tit);
                    intent.putExtra("bac", ApiConfig.APP_URL + mAdapter.getItem(position).spic);
                    startActivity(intent);
                }

                if (type.equals("体验馆")) {
                    Intent intent = new Intent(getActivity(), VideoListActivity.class);
                    intent.putExtra("postion", position);
                    intent.putExtra("title", mAdapter.getItem(position).news_tit);
                    startActivity(intent);
                }


            }
        });


    }

    private void getData(String type) {
        if (type.equals("听书馆")) {

            showLoading(getActivity());
            ViseHttp.GET("index.php/Jiekou/yinpins")
                    .tag(TAG)
                    //.setHttpCache(true)
                    //配置读取超时时间，单位秒
                    .readTimeOut(60)
                    //配置写入超时时间，单位秒
                    .writeTimeOut(60)
                    //配置连接超时时间，单位秒
                    .connectTimeOut(60)
                    //配置请求失败重试次数
                    .retryCount(3)
                    //配置请求失败重试间隔时间，单位毫秒
                    .retryDelayMillis(1000)

                    .setLocalCache(true)
                    .cacheKey("listenBook")
                    .cacheTime(10 * 60 * 60 * 60 * 1000)
                    .cacheMode(CacheMode.FIRST_CACHE)//先加载缓存，缓存没有再去请求网络
                    .request(new ACallback<CacheResult<List<MultiMediaBean>>>() {
                        @Override
                        public void onSuccess(CacheResult<List<MultiMediaBean>> cacheResult) {
                            cancelLoading();
                            //请求成功，AuthorModel为解析服务器返回数据的对象，可以是String，按需定义即可
                            List<MultiMediaBean> bookListBeen = cacheResult.getCacheData();
                            if (bookListBeen != null && bookListBeen.size() > 0) {
                                Log.i(TAG, "onSuccess: bookListBeen.size()=" + bookListBeen.size());
                                mAdapter.setNewData(bookListBeen);
                            } else {
                                mAdapter.setNewData(null);
                                mAdapter.setEmptyView(R.layout.comment_empty_view, (ViewGroup) mRecyclerView.getParent());
                            }
                        }

                        @Override
                        public void onFail(int errCode, String errMsg) {
                            cancelLoading();
                            //请求失败，errCode为错误码，errMsg为错误描述
                            ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                        }
                    });


        } else if (type.equals("体验馆")) {

         showLoading(getActivity());
            ViseHttp.GET("index.php/Jiekou/shipin")
                    .tag(TAG)
                    //.setHttpCache(true)
                    //配置读取超时时间，单位秒
                    .readTimeOut(60)
                    //配置写入超时时间，单位秒
                    .writeTimeOut(60)
                    //配置连接超时时间，单位秒
                    .connectTimeOut(60)
                    //配置请求失败重试次数
                    .retryCount(3)
                    //配置请求失败重试间隔时间，单位毫秒
                    .retryDelayMillis(1000)

                    .setLocalCache(true)
                    .cacheKey("video")
                    .cacheTime(10 * 60 * 60 * 60 * 1000)
                    .cacheMode(CacheMode.FIRST_CACHE)//先加载缓存，缓存没有再去请求网络
                    .request(new ACallback<CacheResult<List<MultiMediaBean>>>() {
                        @Override
                        public void onSuccess(CacheResult<List<MultiMediaBean>> cacheResult) {
                         cancelLoading();
                            //请求成功，AuthorModel为解析服务器返回数据的对象，可以是String，按需定义即可
                            List<MultiMediaBean> bookListBeen = cacheResult.getCacheData();
                            if (bookListBeen != null && bookListBeen.size() > 0) {
                                Log.i(TAG, "onSuccess: bookListBeen.size()=" + bookListBeen.size());
                                mAdapter.setNewData(bookListBeen);
                            } else {
                                mAdapter.setNewData(null);
                                mAdapter.setEmptyView(R.layout.comment_empty_view, (ViewGroup) mRecyclerView.getParent());
                            }
                        }

                        @Override
                        public void onFail(int errCode, String errMsg) {
                           cancelLoading();
                            //请求失败，errCode为错误码，errMsg为错误描述
                            ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                        }
                    });
        }


    }

}




